<template>
	<view>
		<view class="swiper-back">
			<view class="app-title"></view>
			<view class="swiper-box">
				<u-swiper :list="bannerList" height="260" mode="none" border-radius="20" @click="clickBanner"></u-swiper>
			</view>
		</view>
		<view class="home-page">
			<view class="u-m-b-30">
				<view class="notice-box" @click="toNoticeList">
					<view class="notice-icon"></view>
					<view class="notice-view">
						<u-notice-bar type="none" :volume-icon="false" :more-icon="true" color="#333333" speed="80" :is-circular="noticeList[0].length > 10"
						 :list="noticeList"></u-notice-bar>
					</view>
				</view>
			</view>
			<view class="news-list">
				<view class="news-title"> 新闻动态 </view>
				<view class="news-list-item" v-for="(item, i) in newsList" :key="i" @click="clickNews(item)">
					<view class="news-item-dot"></view>
					<view class="news-item-title">{{ item.title }}</view>
					<view class="news-item-time">{{
            $moment(item.pubdate).format("MM-DD")
          }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	// 记录步数
	let StepCounter = uni.requireNativePlugin("YStep-Counter");
	
	export default {
		data() {
			return {
				bannerList: [],
				newsList: [],
				workStepFlag: false,
				afterToWorkFlag: false,
			};
		},
		computed: {
			noticeList() {
				return [this.$store.state.notice_msg_def.noticeDefaultText];
			},
		},
		methods: {
			clickBanner(pIndex) {
				const {
					url,
					title
				} = this.bannerList[pIndex];
				this.$u.route("/pages/home/webpage", {
					url: url,
					title: title,
				});
			},
			clickNews(pNews) {
				const {
					url,
					title
				} = pNews;
				this.$u.route("/pages/home/webpage", {
					url: url,
					title: title,
				});
			},
			toNoticeList() {
				this.$u.route("/pages/message/messagecenter/noticelist");
			},
			getTime(time) {
				let Time = this.$u.timeFormat(new Date(new Date().getTime()), "yy/mm/dd") + " " + time;
				return new Date(Time).getTime();
			},
			isTodayPushch(time) {
				let start = new Date(new Date().toLocaleDateString()).getTime();
				let end =
					new Date(new Date().toLocaleDateString()).getTime() +
					24 * 60 * 60 * 1000;
				return time > start && time < end;
			},
			// 判断上下班是否打卡
			setPushChTime() {
				let goToWorkTime = uni.getStorageSync("goWorkTimeRecodrd");
				let offWorkTime = uni.getStorageSync("offWorkTimeRecodrd");
				if (
					goToWorkTime &&
					goToWorkTime.date &&
					goToWorkTime.date < this.goToWorkTime
				) {
					if (this.isTodayPushch(goToWorkTime.date)) {
						this.goToWorkTime = goToWorkTime.date;
					} else {
						uni.removeStorageSync("goWorkTimeRecodrd");
					}
				}
				if (offWorkTime && offWorkTime.date && offWorkTime.date < this.offWorkTime) {
					if (this.isTodayPushch(offWorkTime.date)) {
						this.offWorkTime = offWorkTime.date;
					} else {
						uni.removeStorageSync("offWorkTimeRecodrd");
					}
				}
			},
			getTodayTime () {
				this.$u.api.getScheduling().then(res => {
					if (res && res.goToWorkTime) {
						this.goToWorkTime = this.getTime(res.goToWorkTime)+15*60*1000
						this.offWorkTime = this.getTime(res.offWorkTime)+30*60*1000
						this.isWorkDay = res.isWorkDay
					} else {
						this.isWorkDay = 0;
					}
					this.setTimer()
				}).catch(err => {
					this.isWorkDay = 0;
					this.setTimer()
				})
			},
			setStep() {
				// console.log("开始记步");
				if (!uni.getStorageSync("NotWorkStep")) {
					uni.setStorageSync("NotWorkStep", {});
				}
				if (!uni.getStorageSync("WorkStep")) {
					uni.setStorageSync("WorkStep", {});
				}

				this.getTodayTime()
				// this.goToWorkTime = this.getTime("8:30:00");
				// this.offWorkTime = this.getTime("9:00:00");
				// this.isWorkDay = 1
				// this.setTimer()
				this.sendStepData(6);
			},
			setTimer() {
				clearInterval(this.timer);
				this.timer = setInterval(() => {
					// 无排班或者放假
					if (this.isWorkDay === 0) {
						// console.log("节假日,计步中")
						let goToWorkTime = uni.getStorageSync("goWorkTimeRecodrd");
						// 打上班卡
						if (
							goToWorkTime &&
							goToWorkTime.date
							&&this.isTodayPushch(goToWorkTime.date)
						) {
							this.goToWorkTime = goToWorkTime.date
							this.offWorkTime = goToWorkTime.date+8*60*60*1000 // 上班顺延八小时为下班时间
							
							this.computeStep();
						}else{
							this.computedNotWorkStep();
						}
						
					} else {
						// 有排班
						this.computeStep();
					}
					if (
						this.$u.timeFormat(new Date(new Date().getTime()), "hh:MM:ss") ===
						"23:59:00"
					) {
						this.sendStepData(5);
						uni.removeStorageSync("goWorkTimeRecodrd");
						uni.removeStorageSync("offWorkTimeRecodrd");
						uni.setStorageSync("WorkStep", {});
						uni.setStorageSync("NotWorkStep", {});
					}
					if (
						this.$u.timeFormat(new Date(new Date().getTime()), "hh:MM:ss") ===
						"21:30:00"
					) {
						this.sendStepData(6);
					}
				}, 1000);
			},
			computeStep() {
				let time = new Date().getTime();
				if(this.goToWorkTime<new Date(new Date().toLocaleDateString()).getTime()) {
					this.getTodayTime()
				}
				if (this.isWorkDay !== 0) {
					this.setPushChTime();
				}
				// console.log(uni.getStorageSync("goWorkTimeRecodrd"),new Date(this.goToWorkTime),new Date(this.offWorkTime))
				if (this.goToWorkTime < this.offWorkTime) {
					if (time > this.goToWorkTime && time < this.offWorkTime) {
						this.setWorkStep();
					} else {
						this.computedNotWorkStep();
						// if (!this.afterToWorkFlag) {
						// 	this.sendStepData(3);
						// 	this.afterToWorkFlag = true;
						// }
					}
				} else {
					// 上班时间大于下班时间 隔天（后台返回的是时分秒的时间戳)
					if (
						time > this.goToWorkTime &&
						time < this.offWorkTime + 24 * 60 * 60 * 1000
					) {
						this.setWorkStep();
					} else {
						// if (!this.workStepFlag) {
						// 	this.sendStepData(3);
						// 	this.workStepFlag = true;
						// }
						// 上班前20分钟内更新一次步数
						// if(time < this.goToWorkTime && time< this.goToWorkTime - 20*60*1000) { }
						this.computedNotWorkStep();
					}
				}
			},
			// 记录上班步数
			setWorkStep() {
				// console.log("shangban")
				let that = this;
				let data = uni.getStorageSync("NotWorkStep");
				// console.log(data)
				if (!data.calorie) {
					data.calorie = 0;
					data.distance = 0;
					data.numberOfSteps = 0;
					data.date = new Date().getTime();
				}
				StepCounter.setStepCallback(function(res) {});
				StepCounter.queryStepCounter(function(res) {
					if(!(that.isTodayPushch(data.date))) {
						uni.setStorageSync("NotWorkStep",{});
					}
					
					let WorkStep = {
						calorie: (Number(res.distance)/16).toFixed(0) - (data.distance/16).toFixed(0),
						distance: Number(res.distance) - data.distance,
						km: ((Number(res.distance) - data.distance) / 1000).toFixed(3),
						numberOfSteps: Number(res.numberOfSteps) - data.numberOfSteps,
						date: new Date().getTime()
					};
					
					uni.setStorageSync("WorkStep", WorkStep);
				});
			},
			
			// 记录下班步数
			computedNotWorkStep() {
				let that = this;
				let data = uni.getStorageSync("WorkStep");
				if (!data.calorie) {
					data.calorie = 0;
					data.distance = 0;
					data.numberOfSteps = 0;
					data.date = new Date().getTime();
				}
				// console.log("下班，开始记步")
				StepCounter.setStepCallback(function(res) {});
				StepCounter.queryStepCounter(function(res) {
					if(!(that.isTodayPushch(data.date))) {
						uni.setStorageSync("WorkStep",{});
					}
					let notWorkStep = {
						calorie: (Number(res.distance)/16).toFixed(0) - (data.distance/16).toFixed(0),
						distance: Number(res.distance) - data.distance,
						km: ((Number(res.distance) - data.distance) / 1000).toFixed(3),
						numberOfSteps: Number(res.numberOfSteps) - data.numberOfSteps,
						date: new Date().getTime()
					};
					
					uni.setStorageSync("NotWorkStep", notWorkStep);
				});
			},
			sendStepData(type) {
				let NotWorkStep = uni.getStorageSync("NotWorkStep");
				if (!NotWorkStep.calorie) {
					NotWorkStep.calorie = 0;
					NotWorkStep.numberOfSteps = 0;
					NotWorkStep.distance = 0;
				}
				let params = {
					userId: uni.getStorageSync("lifeData").vuex_user.userInfo.id,
					steps: NotWorkStep.numberOfSteps,
					type: type,
					distance: NotWorkStep.distance,
					km: NotWorkStep.km,
					clockTime: this.$u.timeFormat(new Date(), "yyyy-mm-dd hh:MM:ss"),
				};
				console.log(NotWorkStep, params);
				this.$u.api.forestPunchRecord(params).then((res) => {
					//成功记录步数信息})
				});
			},
		},
		onLoad() {
			this.$u.api
				.getBanner()
				.then((res) => {
					console.log(res)
					for (const item of res.list) {
						this.bannerList.push({
							image: this.$u.http.config.baseUrl + JSON.parse(item.path)[0].url,
							url: item.url,
							title: item.title,
						});
					}
				})
				.catch((err) => {
					console.log(err);
				});
			this.$u.api
				.getNews()
				.then((res) => {
					this.newsList = res.list;
					console.log(res);
				})
				.catch((err) => {
					console.log(err);
				});
				this.setStep();
		},
	};
</script>

<style lang="scss" scoped>
	.swiper-back {
		position: relative;
		height: calc(200rpx + 88rpx);
		overflow: hidden;

		.app-title {
			position: relative;
			left: -20%;
			width: 140%;
			height: 80%;
			border-radius: 0 0 50% 50%;
			background-color: #ffffff;
			// background: linear-gradient(121deg, #22ABEC 0%, #0071C5 100%);
		}

		// .app-title:after { width: 140%; height: 100px; position: absolute; left: -20%; top: 0; z-index: -1; content: ''; border-radius: 0 0 50% 50%; background: linear-gradient(#2CBAF9, #04A9F4); }
		.swiper-box {
			position: absolute;
			margin: 0 auto;
			left: 0;
			right: 0;
			bottom: 0;
			width: calc(100% - 60rpx);
		}
	}

	.home-page {
		padding: 30rpx;

		.news-list {
			border-radius: 20rpx;
			background-color: #ffffff;

			.news-title {
				padding: 30rpx 44rpx 18rpx 44rpx;
				font-size: 32rpx;
				font-weight: bold;
			}

			.news-list-item {
				display: flex;
				min-height: 96rpx;
				border-bottom: 2rpx solid #f7f8fa;

				.news-item-dot {
					margin: 43rpx 20rpx;
					width: 10rpx;
					height: 10rpx;
					border-radius: 5rpx;
					background-color: #0071c5;
				}

				.news-item-title {
					flex: 1;
					padding: 20rpx;
					font-size: 28rpx;
				}

				.news-item-time {
					width: 100rpx;
					line-height: 96rpx;
					font-size: 24rpx;
					font-weight: bold;
					color: #999999;
				}
			}
		}

		.notice-box {
			display: flex;
			height: 72rpx;
			border-radius: 20rpx;
			background-color: #ffffff;

			.notice-icon {
				width: 126rpx;
				height: 72rpx;
				background: url(../../static/icons/home-notice-icon.png) no-repeat;
				background-size: cover;
			}

			.notice-view {
				flex: 1;
				width: 0;
			}
		}
	}
</style>
